#pragma once
#include <wx/ctb/serport.h>

static const int PACKET_LEN	=	512;
static const int MAX_BLOCKLEN = 256;

static const unsigned char	ESCAPE_FRAME	    =	0xF0;
static const unsigned char	BEGIN_FRAME		    =	0xF1;
static const unsigned char	END_FRAME	    	=	0xF2;

static const unsigned char TW_COMMAND			=	0x1;
static const unsigned char TW_DATA				=	0x2;
static const unsigned char TW_STREAM_DATA		=	0x6;

static const unsigned char TW_AUTHENTICATE		=	0xE1;
static const unsigned char TW_ACK				=	0xE0;
static const unsigned char TW_SENDALARMREQUEST	=	0xE5;
static const unsigned char TW_CONFIGREQUEST	    =	0xE6;
static const unsigned char TW_CLOSE			    =	0xEA;


static const unsigned char TW_CONFIGURE         =   0xEB;
static const unsigned char TW_DOWNLOADFILE      =   0xE3;
static const unsigned char TW_STARTSTOP         =   0xEC;
static const unsigned char TW_STARTSYNTH        =   0xED;


static const int			TIMEOUT	=	3000000;

//#pragma pack(2)
typedef struct TW_Command
{
	unsigned char	ProtocolID;	
	unsigned char	CommandID;
	unsigned int	SenderID;
	unsigned short	Params[16];
}TW_Command;

//#pragma pack(1)
typedef struct TW_ShortData
{
	unsigned char	ProtocolID;	//0x5
	unsigned char	StreamType;//0x1
	unsigned int	SenderID;
	unsigned short	PieceNumber;
	unsigned short	HR;
	unsigned short	HRQ;
	unsigned short	StepsMin;
	unsigned short	PostureActivity;
	unsigned short	StrokeVolume;
	unsigned short	CO;
	unsigned int	FutureUse;
}TW_ShortData;

//#pragma pack(2)
typedef struct TW_ObjData
{
	unsigned char	ProtocolID;	//0x2
    unsigned char	ObjKind;//0x1
    unsigned int	SenderID;
	unsigned short	PieceNumber;
	unsigned short	DataLen;
    unsigned char   Data[MAX_BLOCKLEN];//up to???
}TW_ObjData;

//#pragma pack(2)
typedef struct TW_StreamingData
{
	unsigned char	ProtocolID;	//0x6
	unsigned char	StreamType;//0x1
	unsigned int	SenderID;
	unsigned int	PieceNumber;
	unsigned short	ChannelFlags;
	unsigned short	BlockLen;
	unsigned short	Data[MAX_BLOCKLEN];
}TW_StreamingData;

int VerifyCRC(unsigned char *pData,int Len);
int StuffData(unsigned char *pUnstuffed,int Len,unsigned char *pStuffed);

int UnstuffData(unsigned char *pData,int Len,unsigned char *pUnstuffed);

void    SendObjData(TW_ObjData *pData,int FrameNumber);
void    SendCommand(TW_Command *pData,int FrameNumber);

/*int DecodeFrame(unsigned char *pData,short int Len,unsigned char *PayLoad);
int RecvFrame(unsigned char *pData);*/
bool ReceiveObjData(TW_ObjData	*pData);
bool ReceiveStreamData(TW_StreamingData	*pData);
bool ReceiveCommand(TW_Command	*pAck);


